//
//  ReviewDetailsPage_Vc.swift
//  MikerShop
//
//  Created by eme on 2016/12/1.
//  Copyright © 2016年 eme. All rights reserved.
//

/*
 owner:cy
 update:2016年12月01日11:11:45
 info: 审核详情
 
 */
import RxSwift
import Kingfisher
import SwiftyUserDefaults
import UtilCore
import ModelProtocol
import URLNavigator
import RickonCore
import BrandonCore

class ReviewDetailsPage_Vc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    ///审核状态
    @IBOutlet weak var spstate_Lb: UILabel!
    
    @IBOutlet weak var header_SeperateLine_V: UIView!
    @IBOutlet weak var top_SeperateLine_V: UIView!
    @IBOutlet weak var bottom_SeperateLine_V: UIView!
    /// 企业名称
    @IBOutlet weak var spcompany_Lb: UILabel!
    /// 联系人姓名
    @IBOutlet weak var spcontact_Lb: UILabel!
    /// 手机号
    @IBOutlet weak var spmobile_Lb: UILabel!
    /// 邀请码
    @IBOutlet weak var aginvitecode_Lb: UILabel!
    /// 所在省市区
    @IBOutlet weak var address_Lb: UILabel!
    /// 营业执照编号
    @IBOutlet weak var splicense_Lb: UILabel!
    
    @IBOutlet weak var splicenseurl_ImgV: UIImageView!
    /// 申请时间
    @IBOutlet weak var spregdate_Lb: UILabel!
    
    /// 审核不通过的原因
    @IBOutlet weak var spstate_Result_Lb: UILabel!
    /// 不通过原因
    @IBOutlet weak var show_result_Lb: UILabel!
    
    @IBOutlet weak var submit_Btn: UIButton!
    /// 按钮视图区域
    @IBOutlet weak var submit_V: UIView!
    
    @IBOutlet weak var submit_V_Height_LC: NSLayoutConstraint!
    /*----------------------------   自定义属性区域    ----------------------------*/
    var manageVm:ReviewDetailsPage_Vm?
    var model:GlShopModel?
    var spid:String?
    /****************************Storyboard 绑定方法区域****************************/
    
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
    }
    /**
     app 主题 设置
     */
    override func setViewTheme(){
        self.header_SeperateLine_V.backgroundColor = Theme.v.separate_Line
        self.top_SeperateLine_V.backgroundColor = Theme.v.separate_Line
        self.bottom_SeperateLine_V.backgroundColor = Theme.v.separate_Line
        self.submit_Btn.changeBorderColor(UIColor.clear, cornerRadius: 3.5)
        self.submit_Btn.backgroundColor = UIColor.clear
        self.submit_Btn.changeBackgroundImage(Theme.btn.normal, highlightedColor: Theme.btn.highlighted, disabledColor: Theme.btn.disabled)
    }
    /**
     自定义返回上级界面
     */
    override  func backToView()  {
        _ = self.navigationController?.popToRootViewController(animated: true)
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        
        self.manageVm = ReviewDetailsPage_Vm(refreshTriger: rx.sentMessage(#selector(ReviewDetailsPage_Vc.viewWillAppear)).map { _ in self.spid ?? "" })
        self.manageVm?.glShopItemSubject
            .subscribe(onNext: {[unowned self] (json) in
                self.updateCustomView(json)
            }).addDisposableTo(disposeBag)
        self.manageVm?.loading.bindTo(self.view.rx_loading).addDisposableTo(disposeBag)
        self.submit_Btn.rx.tap.subscribe(onNext: { [unowned self] ( _ ) in
            if let model = self.model{
                switch model.spstate {
                case "1":
                    /// 审核成功 进入觅客直供商城 ， 这块需要将hasLogined 赋值为 true
                    Defaults[.hasLogined] = true
                    ///  这块需要直接跳转到首页
                    _ = "loginsuccess".openURL()
                case "4","5","6":
                    /// 审核失败.. 需要跳转到填写资料的界面
                    _ = "registerdetailinfo".openURL(["spid":model.spid])
                    print(" 审核失败..")
                default:
                    break
                }
            }
        }).disposed(by: disposeBag)
        self.manageVm?.error.asObserver().subscribe(onNext: { (error) in
            self.updateCustomView(GlShopModel(json: "")!)
            self.view.toastError(error)
        }).disposed(by: disposeBag)
    }
    
    /// 更新界面显示
    func updateCustomView(_ glShop:GlShopModel)  {
        self.model = glShop
        self.spstate_Lb.text = glShop.showSpstate
        self.spcompany_Lb.text = glShop.spcompany
        self.spcontact_Lb.text = glShop.spcontact
        self.aginvitecode_Lb.text = glShop.aginvitecode
        self.spmobile_Lb.text = glShop.spmobile
        self.address_Lb.text =  "\(glShop.agprovincename + glShop.spareaname + glShop.agareaname)"
        self.splicense_Lb.text = glShop.splicense
        self.spregdate_Lb.text = glShop.spregdate
        self.splicenseurl_ImgV.setUrlImage(glShop.splicenseurl)
        ///spstate状态:1-正常(审核通过) 2-代理审核 3-入驻审核 4-冻洁 5-代理审核失败 6-入驻审核失败
        switch glShop.spstate {
        case "1":
            /// 审核成功
            self.updateSbumitView(isHidden: false)
            self.show_result_Lb.isHidden = true
            self.spstate_Result_Lb.isHidden = true
            self.submit_Btn.changeBorderColor(UIColor.clear, cornerRadius: 3.5)
            self.submit_Btn.changeBackgroundImage(Theme.btn.normal, highlightedColor: Theme.btn.highlighted, disabledColor: Theme.btn.disabled)
            self.submit_Btn.setTitleColor(UIColor.white, for: .normal)
            self.submit_Btn.setTitle("进入直供宝商城", for: .normal)
        case "4","5","6":
            /// 审核失败..
            self.updateSbumitView(isHidden: false)
            self.show_result_Lb.isHidden = false
            self.spstate_Result_Lb.isHidden = false
            self.spstate_Lb.textColor = Theme.btn.normal
            self.submit_Btn.setTitle("重新提交资料", for: .normal)
            self.spstate_Result_Lb.text = glShop.auditremark
            self.submit_Btn.changeBorderColor(Theme.btn.normal, cornerRadius: 3.5)
            self.submit_Btn.setTitleColor(Theme.btn.normal, for: .normal)
            self.submit_Btn.changeBackgroundImage(UIColor(white: 1.0, alpha: 1.0), highlightedColor: UIColor(white:1.0, alpha: 0.3), disabledColor: UIColor(white:1.0, alpha: 0.3))
        default:
            // 表示正在审核中
            self.spstate_Lb.textColor = Theme.btn.normal
            self.updateSbumitView(isHidden: true)
            self.show_result_Lb.isHidden = true
            self.spstate_Result_Lb.isHidden = true
        }
        
        
    }
    func updateSbumitView(isHidden:Bool) -> Void {
        self.submit_V.isHidden = isHidden
        if isHidden {
            self.submit_V_Height_LC.constant = 0.0
        } else {
            self.submit_V_Height_LC.constant = self.view.frame.size.width * 0.14
        }
    }
    
}
